﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace DAL
{
    /// <summary>
    ///Convert 的摘要说明
    /// </summary>
    public class NumFormat
    {
        public NumFormat()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }

        /// <summary>
        /// decimail类型转换
        /// </summary>
        /// <param name="num">转换对象</param>
        /// <param name="Formate">输出格式</param>
        /// <returns></returns>
        public static string ConvertStringDecimal(Decimal? num, string Formate)
        {
            if (num != null)
            {
                return ((Decimal)num).ToString(Formate);
            }
            else
            {
                return "--";
            }
        }

        /// <summary>
        /// decimail类型转换
        /// </summary>
        /// <param name="num">转换对象</param>
        /// <returns></returns>
        public static string ConvertStringDecimal(Decimal? num)
        {
            if (num != null)
            {
                return ((Decimal)num).ToString();
            }
            else
            {
                return "--";
            }
        }

        /// <summary>
        /// int类型转换
        /// </summary>
        /// <param name="num">转换对象</param>
        /// <returns></returns>
        public static string ConvertStringInt(int? num)
        {
            if (num != null)
            {
                return ((int)num).ToString();
            }
            else
            {
                return "--";
            }
        }

        /// <summary>
        /// DateTime类型转换
        /// </summary>
        /// <param name="num">转换对象</param>
        /// <returns></returns>
        public static string ConvertStringDateTime(DateTime? num, string Format)
        {
            if (num != null)
            {
                string dt = ((DateTime)num).ToString(Format);
                if (dt == "1900-01-01")
                {
                    dt = "--";
                }
                return dt;
            }
            else
            {
                return "--";
            }
        }

        public static Decimal? ConvertDecimal(string num)
        {
            Decimal? re;
            Decimal ret;
            if (!Decimal.TryParse(num, out ret))
            {
                re = null;
            }
            else
            {
                re = ret;
            }
            return re;
        }

        public static int? ConvertInt(string num)
        {
            int? re;
            int ret;
            if (!int.TryParse(num, out ret))
            {
                re = null;
            }
            else
            {
                re = ret;
            }
            return re;
        }

        public static DateTime? ConvertDateTime(string date)
        {
            DateTime? re;
            DateTime ret;
            if (!DateTime.TryParse(date, out ret))
            {
                re = null;
            }
            else
            {
                re = ret;
            }
            return re;
        }

        public static string FormatNum10000(string arg)
        {
            int num = 0;
            if (!int.TryParse(arg, out num))
            {
                return "--";
            }
            return (num / 10000).ToString();
        }

        public static string FormatNum10000(int? arg)
        {
            //int num = 0;
            if (arg == null)
            {
                return "--";
            }
            return (arg / 10000).ToString();
        }

        public static string FormatNum10000(long? arg)
        {
            //int num = 0;
            if (arg == null)
            {
                return "--";
            }
            return (arg / 10000).ToString();
        }

        public static int GetInt(int? arg)
        {
            if (arg == null)
            {
                return 0;
            }
            else
            {
                return (int)arg;
            }
        }

        public static decimal GetDecimal(decimal? arg)
        {
            if (arg == null)
            {
                return 0;
            }
            else
            {
                return (decimal)arg;
            }
        }

        public static DateTime ToDateTime(object obj, DateTime v)
        {
            try
            {
                v = DateTime.Parse(obj.ToString());
            }
            catch { }
            return v;
        }
    }
}